package com.huawei.appmarket.service.distribution.activereport;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import com.huawei.appmarket.service.deamon.bean.DownloadHistory;
import com.huawei.appmarket.support.storage.referrer.AppReferrerRecord;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.util.LinkedHashMap;
import java.util.List;
import o.dgh;
import o.Logger;
import o.dof;
import o.ContentManager;
import o.dsw;
import o.dxd;
import o.dxe;
import o.erw;
import o.eun;

public class AppActiveReportReceiver extends BroadcastReceiver {
    private dxd ˊ;
    private String ॱ = "AppActiveReportReceiver";

    public void onReceive(Context context, Intent intent) {
        SafeIntent safeIntent = new SafeIntent(intent);
        Uri data = safeIntent.getData();
        if (data == null) {
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            linkedHashMap.put("errMsg", "packageUri is null");
            dgh.ˋ("400102", linkedHashMap);
            return;
        }
        String schemeSpecificPart = data.getSchemeSpecificPart();
        Logger.ˎ(this.ॱ, "receive a broadcast:" + safeIntent.getAction() + " package name=" + schemeSpecificPart);
        String ˏ = erw.ˏ(context.getPackageManager(), schemeSpecificPart);
        Logger.ˎ(this.ॱ, "installerPackageName=" + ˏ);
        if (ˏ == null || !ˏ.equals(context.getPackageName())) {
            Logger.ˎ(this.ॱ, "not installed by app market");
            return;
        }
        DownloadHistory ˏ2 = dsw.ˋ().ˏ(schemeSpecificPart);
        if (ˏ2 == null) {
            Logger.ˎ(this.ॱ, "unable to get detailId from download history");
            ˏ = schemeSpecificPart;
        } else {
            ˏ = ˏ2.ᐝ();
            if (TextUtils.isEmpty(ˏ)) {
                ˏ = "detailId_is_empty";
            }
        }
        ˊ(schemeSpecificPart, ˏ, ˏ2);
        new dxe().ˊ(schemeSpecificPart, ˏ);
        if (ˏ2 != null) {
            ˏ = ˏ2.ॱ("advPlatform");
            Logger.ˎ(this.ॱ, "advPlatform=" + ˏ);
            if (String.valueOf(1).equals(ˏ)) {
                Logger.ˎ(this.ॱ, "sent app active info to pps");
                ˏ = ˏ2.ॱ("advInfo");
                if (ˏ == null) {
                    Logger.ˋ(this.ॱ, "unable to get advInfo from download history");
                }
                String ˎ = ˎ(schemeSpecificPart, ˏ2.ॱ("mediaPkg"));
                if (ˎ == null) {
                    Logger.ˋ(this.ॱ, "unable to get referrer");
                }
                ˏ(schemeSpecificPart, ˏ, ˎ);
            }
        }
    }

    private void ˊ(String str, String str2, DownloadHistory downloadHistory) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        Object obj = HwAccountConstants.NULL;
        Object obj2 = HwAccountConstants.NULL;
        if (downloadHistory != null) {
            obj = downloadHistory.ॱ("referrer");
            obj2 = String.valueOf(downloadHistory.ͺ());
        }
        linkedHashMap.put("pkgName", str);
        linkedHashMap.put("detailID", str2);
        linkedHashMap.put("referrer", obj);
        linkedHashMap.put("taskSubmitTime", obj2);
        linkedHashMap.put("activeTime", String.valueOf(System.currentTimeMillis()));
        dgh.ˋ("400101", linkedHashMap);
    }

    private String ˎ(String str, String str2) {
        Logger.ˎ(this.ॱ, "pkgName = " + str + " mediaPkg=" + str2);
        List ˊ = eun.ˏ().ˊ(str, str2);
        if (ˊ.size() == 1) {
            return dof.ˊ(((AppReferrerRecord) ˊ.get(0)).ˎ());
        }
        Logger.ˋ(this.ॱ, "unable to get referrer, size=" + ˊ.size());
        return null;
    }

    private void ˏ(String str, String str2, String str3) {
        if (this.ˊ == null) {
            this.ˊ = new dxd(str, str2, str3);
            Intent intent = new Intent("com.huawei.android.hms.CHANNEL_SERVICE");
            intent.setPackage("com.huawei.hwid");
            ContentManager.getInstant().getContext().bindService(intent, this.ˊ, 1);
            return;
        }
        this.ˊ.ˊ(str, str2, str3);
    }
}
